home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------------------------
- *
- * Simple Sample Application Framework
- *
- * ©1991 Apple Computer
- *
- -------------------------------------------------------------------------------------*/
- /*
- * utils.c -- utility routines
- *
- * change history:
- *
- * SJF 11/6/91 1.0d1 initial coding
- *
- */
-
- #include <GestaltEqu.h>
- #include <Traps.h>
- #include <Notification.h>
-
- #include "const.h"
- #include "globals.h"
- #include "trapavailable.h"
- #include "main.h"
- #include "mymenus.h"
-
- #include "utils.h"
-
- Boolean HasColorQD(void)
- {
- SysEnvRec theEnv;
-
- if (SysEnvirons(1,&theEnv) != noErr)
- return false;
-
- return (theEnv.hasColorQD);
- }
-
-
- void DoError(OSErr err)
- {
- Str255 errStr;
- Str255 errNumStr;
-
- if (err==noErr)
- return;
-
- NumToString(err,errNumStr);
- pstrcpy(errStr,"\pAn error has occurred: ");
- pstrcat(errStr,errNumStr);
-
- Notify(errStr);
- // DebugStr(errStr);
- }
-
-
- void Notify(StringPtr string)
- {
- NMRecPtr nm;
- StringPtr strPtr;
-
- nm = (NMRecPtr)NewPtr(sizeof(NMRec));
- if (MemError()!=noErr)
- return;
- strPtr = (StringPtr)NewPtr(string[0]);
- if (MemError()!=noErr)
- return;
- BlockMove(string,strPtr,string[0]+1);
-
- nm->qType = nmType;
- nm->nmMark = 0;
- nm->nmIcon = nil;
- nm->nmSound = nil;
- nm->nmStr = strPtr;
- nm->nmResp = nil;
- NMInstall(nm);
- }
-
-
- void pstrcpy(void *dest,void *src)
- {
- unsigned char srcLen = ((unsigned char *)src)[0];
-
- BlockMove(src,dest,srcLen+1);
- }
-
-
- void pstrcat(void *original,void *catStr)
- {
- short length;
- unsigned char originalLen = ((unsigned char *)original)[0];
- unsigned char catStrLen = ((unsigned char *)catStr)[0];
-
- length = (short) originalLen;
- length += (short) catStrLen;
-
- if (length > 255) {
- DebugStr("\pstring catenation overflow");
- ExitProc();
- }
-
- BlockMove((char *)catStr+1,(char *)original+originalLen+1,catStrLen);
- ((unsigned char *)original)[0] = (unsigned char) length;
- }
-
-
- void *NewPtrChk(Size ptrSize)
- {
- Ptr thePtr;
-
- thePtr = NewPtr(ptrSize);
- if (MemError()!=noErr)
- DoError(MemError());
- #if kDEBUG
- {
- long *longPtr = (long *)thePtr;
- *longPtr = kBetterBusErr;
- }
- #endif
- return thePtr;
- }
-
-
- void *NewHandleChk(Size hndlSize)
- {
- Handle theHndl;
-
- theHndl = NewHandle(hndlSize);
- if (MemError()!=noErr)
- DoError(MemError());
- #if kDEBUG
- {
- long **longHndl = (long **)theHndl;
- **longHndl = kBetterBusErr;
- }
- #endif
- return theHndl;
- }
-
-
- void DisposPtrChk(void *thePtr)
- {
- #if kDEBUG
- {
- long *longPtr = (long *)thePtr;
- *longPtr = kBetterBusErr;
- }
- #endif
-
- DisposPtr(thePtr);
- if (MemError()!=noErr)
- DoError(MemError());
- }
-
-
- void DisposHandleChk(void *theHndl)
- {
- #if kDEBUG
- {
- long **longHndl = (long **)theHndl;
- **longHndl = kBetterBusErr;
- }
- #endif
-
- DisposHandle(theHndl);
- if (MemError()!=noErr)
- DoError(MemError());
- }
-
-
- OSErr WaitPBDone(void *voidBlock)
- {
- IOParam *pBlock;
-
- pBlock = (IOParam *)voidBlock;
- while (pBlock->ioResult > 0)
- Yield(0);
- return pBlock->ioResult;
- }
-
-
- void Yield(long sleepTime)
- {
- EventRecord ev;
-
- EventAvail(everyEvent,&ev);
- }
-
-
- void ExitProc(void)
- {
- ExitToShell();
- }
-
-
- void DisableAllMenus(void)
- {
- MenuHandle theMenu;
- short menuIndex,itemIndex,numItems;
-
- for (menuIndex=kAppleMenu+1; menuIndex<kAppleMenu+kNumMenus; menuIndex++) {
- theMenu = GetMHandle(menuIndex);
- numItems = CountMItems(theMenu);
- for (itemIndex=1; itemIndex<=numItems; itemIndex++)
- DisableItem(theMenu,itemIndex);
- }
-
- DrawMenuBar();
- }
-
-
- void EnableAllMenuItems(MenuHandle theMenu)
- {
- short itemIndex,numItems;
-
- numItems = CountMItems(theMenu);
- for (itemIndex=1; itemIndex<=numItems; itemIndex++)
- EnableItem(theMenu,itemIndex);
- }
-
-
- void SetDefaultMenus(void)
- {
- MenuHandle theMenu;
-
- DisableAllMenus();
- theMenu = GetMHandle(kFileMenu);
- EnableItem(theMenu,kOpenItem);
- EnableItem(theMenu,kQuitItem);
-
- theMenu = GetMHandle(kEditMenu);
- EnableItem(theMenu,kCutItem);
- EnableItem(theMenu,kCopyItem);
- EnableItem(theMenu,kPasteItem);
- EnableItem(theMenu,kClearItem);
- }